# -*- coding: utf-8 -*-

import pymysql
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.utils.project import get_project_settings


class ViewtagsPipeline(object):
    def __init__(self):
        # reload(sys)
        # sys.setdefaultencoding('utf-8')
        settings = get_project_settings()
        self.connection = pymysql.connect(
            host=settings['PGSQL_HOST'],  # 读取settings中的配置
            database=settings['PGSQL_DBNAME'],
            user=settings['PGSQL_USER'],
            password=settings['PGSQL_PASSWD'],
            port = settings['PGSQL_PORT'],
            charset='utf8',
            # cursorclass=psycopg2.cursor().DictCursor,
            #use_unicode=False,
        )
        self.cursor = self.connection.cursor()

    def process_item(self, item,spider):


        try:
            if item.get('spot_name'):
                self.cursor.execute('insert into cr_dianping_comments_tags(spot_name,comments_tags,tags_count,url) values (%s,%s,%s,%s)',
                    (item['spot_name'], item['comments_tags'], item['tags_count'], item['url']))
                self.connection.commit()
        except Exception as e:
            self.connection.rollback()
            print("Error: %s" % e)
        return item



